Repository navigation
Conversation
Each simulation run can have one cliff calculation that hasn't failed, holding its state and, once it succeeds, the run's cliff gap and cliff share. The tests cover the model, its checks and partial unique index, the revision that adds the table, and the grants: the simulation service starts calculations, the Modal workers finish them, the Public API reads them, and the catalog writer and the Stage 12 role can do neither. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add cliff_impacts (revision bc334f9877b7, autogenerated on f519a77ba122): one row per cliff calculation of a simulation run, with its state and, once it succeeds, the run's cliff gap and cliff share. A partial unique index allows one calculation per run that hasn't failed, so identical requests join it and a failed one never counts. The row is deleted with its run, and inherits the run's versions. Its status reuses v2_simulation_status, the revision's one post-generation correction. The simulation service may insert calculations and record or fail their Modal calls, the Modal workers may write their state and results, the Public API may read them, and no other role may touch them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Stage 9 and Stage 12A runbooks' downgrade chains now start with bc334f9877b7, and the head foreign-key test counts cliff_impacts among the simulation side's tables. The changelog fragment and the skill section say what each role may do, since the simulation API writes the table only once its own change lands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4327 +/- ##
==========================================
+ Coverage 93.63% 93.66% +0.03%
==========================================
Files 408 408
Lines 23282 23297 +15
Branches 3648 3648
==========================================
+ Hits 21799 21822 +23
+ Misses 868 858 -10
- Partials 615 617 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The v2 metadata now holds 51 tables. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 17, 2026
Open
Draft
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 18, 2026
The changelog fragment, the migrations skill, the revision's docstring and two test docstrings now name the service API v1, and the test of its reads is renamed to match. The revision's schema and grants are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dynamics revision 9d1d12a7ac58 (#4325) reached master first on the same parent, so the cliff impacts revision bc334f9877b7 now revises it, and stays the v2 head. Generating it again from 9d1d12a7ac58 gives the same operations. The revision tests list both in chain order, the cliff impacts integration test downgrades to the dynamics revision, and the migrations skill and the stage 9 and stage 12a downgrade notes name the dynamics revision's step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cliff impacts revision now follows the dynamics revision, so the test's upgrade to the dynamics revision from the head did nothing, and its failing downgrade then rolled back the cliff impacts step too, leaving the database at bc334f9877b7. A downgrade to the dynamics revision restores what the test checks: the dynamics downgrade fails and changes nothing while a dynamic is stored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The failed downgrade leaves legacy_policy_mappings.dynamic_id in place, which the dynamics downgrade drops before the step that fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The V2 Postgres migration lifecycle job is to run test_alembic_v2_dynamics.py, and the cross-database integration job test_v2_dynamic_persistence.py, after the managed roles get their privileges, since it runs as the runtime role. Both came with #4325, and no CI job runs either yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The V2 Postgres migration lifecycle script now runs test_alembic_v2_dynamics.py after the cliff impacts module, against its own disposable database, and the cross-database integration job runs test_v2_dynamic_persistence.py in its policy persistence step, after the managed roles get their privileges. Both read only V2_MIGRATION_DATABASE_URL, V2_ALEMBIC_DISPOSABLE_TEST and, for the managed roles, DEPLOYMENT_ENVIRONMENT, which each job's env block already sets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gration job The dynamics migration tests' engine fixture now checks that the target is the disposable database before upgrading it, as the cliff impacts fixture does, so without one the tests are skipped instead of migrating whatever is configured. The integration workflow test now also requires that those migration tests stay out of that job, and says why in its docstring. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4326
Summary
Adds revision
bc334f9877b7to the v2 chain (on9d1d12a7ac58, autogenerated with--rev-id). It createscliff_impacts: one row per cliff calculation of a simulation run, with the calculation's state and, once it succeeds, the run's cliff gap and cliff share. It also declares the table's grants.Design decisions
The simulation API's change that writes the table and its change that starts calculations agreed this design before the revision was built:
status(thev2_simulation_statustype runs use),modal_call_id,error_message(code: message),started_atandcompleted_at. This lets a request tell "no result" from "being calculated" from "done".uq_cliff_impacts_unfailed, partial unique onsimulation_run_idwherestatus <> 'failed', also declared for SQLite).ON CONFLICT (simulation_run_id) WHERE status <> 'failed' DO NOTHING, so two requests at once start one calculation.completed_at; a succeeded row has both numbers, and no other row has either. The numbers aren't range-checked: .py doesn't keep the share to at most 1, and a filtered simulation with no adults gives NaN.CASCADEon the run, aspovertyandinequalityhave. The result is part of its run, and nothing references it.(status, started_at), so the simulation API's stuck-run sweep can fail a calculation whose worker ended without an outcome.Grants (
policyengine_api/data/v2/privileges.py)cliff_impactspolicyengine_v2_simulation_serviceSELECT,INSERT,UPDATE (status, modal_call_id, error_message, completed_at, updated_at): insert a calculation, record its Modal call, or fail it when Modal refuses the callpolicyengine_v2_modal_workerSELECT,UPDATE (status, modal_call_id, error_message, started_at, completed_at, updated_at, cliff_gap, cliff_share): claim, finish or fail; noINSERTpolicyengine_v2_runtimeSELECTpolicyengine_v2_catalog_writer,policyengine_stage12_<environment>Neither simulation API role may change the run a row names or delete a row. The deploy's existing
reconcile_v2_privileges.py applyandverifysteps grant and check these after the schema upgrade; no role needs creating.The revision
statusreuses the baseline'sv2_simulation_statustype (postgresql.ENUM(..., create_type=False)), as1356f658347ddoes forsimulation_runs.status. The generatedcreate_tablewould create the type again and fail. This is the only correction, and it is commented in the revision.simulation_runs.V2_ALEMBIC_HEAD_REVISIONmoves tobc334f9877b7.report_runs' four cliff columns stay until a later revision drops them.f519a77ba122. Store dynamics like policies, create them through /v2/dynamics, and keep gov.simulation parameters out of policies #4325's dynamics revision9d1d12a7ac58, on the same parent, merged first, so this PR mergesmasterand now revises9d1d12a7ac58. Generating it again from there gives the same operations.Docs
docs/engineering/skills/alembic-migrations.md: a section for this revision, plus the two simulation roles' rows in the role table.docs/migration/stage-9-v2-metadata.mdanddocs/migration/stage-12a-user-login.md.CI for #4325's dynamics tests
#4325 added two PostgreSQL modules that no CI job ran. One of its tests broke when this revision moved onto the dynamics revision, and CI didn't catch it.
test_alembic_v2_dynamics.py::test_the_downgrade_fails_changing_nothing_while_a_dynamic_is_storednow downgrades to the dynamics revision instead of upgrading to it, because the head is now later than that revision. It also checks that the failed downgrade leaveslegacy_policy_mappings.dynamic_idin place..github/scripts/test_alembic_v2_lifecycle.shnow runstest_alembic_v2_dynamics.py. These tests downgrade and upgrade the schema, so they run in the V2 Postgres migration lifecycle job, with its own database, like every other revision's migration tests.v2-integration-check.yml's policy persistence step now also runstest_v2_dynamic_persistence.py, after the managed roles get their privileges, since it runs as API v1's runtime role.enginefixture now checks that the target is the disposable database before upgrading it, as the cliff impacts fixture does.tests/unit/test_alembic_workflows.pyrequires both modules where they now run, and that the migration tests stay out of the integration job.Testing
Test-first: the first commit holds the failing tests, seen failing because there was no
CliffImpactmodel, nocliff_impactsgrants and no revisionbc334f9877b7. The tests below were run by node ID against a disposable PostgreSQL 17 database.test_cliff_impact_modelscovers the columns, checks and indexes, and checks that SQLite keeps one unfailed calculation per run and refuses numbers on any row but a succeeded one.test_simulation_run_models' per-simulation result tables.test_alembic_v2_cliff_impacts, PostgreSQL):CASCADE.test_v2_simulation_role_privileges, as the managed roles):test_alembic_v2_simulation_runs): the head foreign-key test now countscliff_impactsamong the simulation side's tables.alembic -c alembic-v2.ini check: no drift;reconcile_v2_privileges.py apply --create-missing-rolesandverify: pass;lint-imports,python -m scripts.rules validate,python scripts/run_quality_guards.py, ruff, ruff format andmypy.policyengine_api/data/v2/modelsfiles reports theField(sa_type=...)overload error on the newstatus,started_atandcompleted_atfields. That is the same error the existingSimulationRunfields have onmaster; the directory is outsidemake typecheck-v2..github/scripts/test_alembic_v2_lifecycle.shand to one step ofv2-integration-check.yml.V2_MIGRATION_DATABASE_URLandV2_ALEMBIC_DISPOSABLE_TEST.alembic-v2-check.ymlsets both as literal job-levelenv:values, for the job's own PostgreSQL service.test_v2_dynamic_persistence.pyreads the same two, plusDEPLOYMENT_ENVIRONMENTfor the managed roles' names, which defaults totest.v2-integration-check.ymlsets all three as literal job-levelenv:values, for its own PostgreSQL service.seed-v2-database.yml, withV2_MIGRATION_DATABASE_URLfrom each GitHub environment's secrets.🤖 Generated with Claude Code